package main

import (
	"fmt"
	"runtime"
	"sort"
)

var stuMap = make(map[int]*stu)

func main() {
	newStu := stu{
		id:    1,
		name:  "chen",
		age:   22,
		score: 100,
	}
	newStu1 := stu{
		id:    2,
		name:  "hello",
		age:   22,
		score: 99,
	}
	newStu2 := stu{
		id:    3,
		name:  "hello",
		age:   22,
		score: 99,
	}
	fmt.Println("新增记录")
	add(newStu)
	add(newStu1)
	add(newStu2)
	//修改前
	fmt.Println("新增后的list")
	list()
	editU := stu{
		id:    2,
		name:  "edit",
		age:   22,
		score: 100,
	}
	fmt.Println("修改id为2的记录")
	edit(editU)
	//修改后
	fmt.Println("修改后的记录list")
	list()
	//删除
	fmt.Println("删除id为3的记录")
	remove(3)
	//删除后
	fmt.Println("删除后的记录list")
	list()

	fmt.Println(runtime.Version())
}

type stu struct {
	id    int
	name  string
	age   int
	score float64
}

func add(s stu) {
	_, isSave := stuMap[s.id]
	if isSave {
		fmt.Println("该学生已存在")
		return
	} else {
		student := &stu{
			id:    s.id,
			name:  s.name,
			age:   s.age,
			score: s.score,
		}
		stuMap[s.id] = student
		fmt.Println("save ok")
	}

}

func edit(s stu) {
	_, isExist := stuMap[s.id]
	if !isExist {
		fmt.Println("该学生不存在")
		return
	} else {
		student := &stu{
			id:    s.id,
			name:  s.name,
			age:   s.age,
			score: s.score,
		}
		stuMap[s.id] = student
		fmt.Println("edit ok")
	}
}

func list() {
	fmt.Printf("\t%s\t%s\t%s\t%s\n", "ID", "姓名", "年龄", "分数")
	sortID := make([]int, 0)
	for k := range stuMap {
		sortID = append(sortID, k)
	}
	sort.Ints(sortID)
	for _, k := range sortID {
		s := stuMap[k]
		fmt.Printf("\t%d\t%s\t%d\t%f\n", s.id, s.name, s.age, s.score)
	}
}

func remove(id int) {
	delete(stuMap, id)
}
